#include "videocapturetask.h"
#include "capturetaskpack.h"
#include "rgbimage.h"
#include "CameraCaptureImpl.h"
#include "videocaptureout.h"
#include "Task_Base/CriticalSection.h"
VideoCaptureTask::VideoCaptureTask()
{
    m_oVideoCapureOut = NULL;
    m_pHandler = NULL;
    m_oCameraCapture = new CameraCaptureImpl;
    m_oYuvCCriticalSection = new CCriticalSection;
}

VideoCaptureTask::~VideoCaptureTask()
{
    close();
    delete m_oCameraCapture;
    delete m_oYuvCCriticalSection;
}

int VideoCaptureTask::Width()
{
    if(m_oCameraCapture)
       return m_oCameraCapture->Width();
    return 0;
}

int VideoCaptureTask::Hight()
{
    if(m_oCameraCapture)
       return m_oCameraCapture->Hight();
    return 0;
}

int VideoCaptureTask::get_camera_list(std::list<string> &sList)
{
    if(m_oCameraCapture)
        m_oCameraCapture->get_camera_list(sList);
    return sList.size();
}

int VideoCaptureTask::get_camera_info_list(const string &camera_name, std::list<string> &listInfo)
{
    if(m_oCameraCapture)
        m_oCameraCapture->get_camera_info_list(camera_name,listInfo);
    return listInfo.size();
}

void VideoCaptureTask::set_capture(VideoCapureOut *pVideoCapureOut)
{
    m_oVideoCapureOut = pVideoCapureOut;
}

bool VideoCaptureTask::Init(const std::string &camera_name,const std::string &info,Receive_handler *pHandler)
{
     m_pHandler = pHandler;

     if(m_oCameraCapture)
         return (m_oCameraCapture->Init(camera_name,info)&& m_oCameraCapture->Start(this));
     return false;
}

int VideoCaptureTask::close()
{
    std::string str("");

    int nthread  =thr_count();

    for (int i =0 ;i<nthread ;i++)
        putq(str);

    wait();

    if(m_oCameraCapture)
    {
        m_oCameraCapture->Stop();
        m_oCameraCapture->UnInit();
    }

    m_pHandler = NULL;
    return true;
}

bool VideoCaptureTask::receive_handler(const char *data, int size)
{
    putq(std::string(data,size));
    return true;
}

void VideoCaptureTask::receive_data(const char *data, int len)
{
    if(m_oVideoCapureOut)
        m_oVideoCapureOut->capure_out(data,len,m_oCameraCapture->Width(),m_oCameraCapture->Hight());

   CCriticalAutoLock AutoLock(*m_oYuvCCriticalSection);
    m_oYuvBuf = std::string(data,len);
}

int VideoCaptureTask::svc()
{
    std::string str ="";

     while(getq(str))
     {
         if (str=="") break;

         CAPTURE_TASK_PACK task_pack;
         memset(&task_pack,0,sizeof(task_pack));

         task_pack.packet = *((AVPacket **)str.data());
         task_pack.width = m_oCameraCapture->Width();
         task_pack.hight = m_oCameraCapture->Hight();
         {
            CCriticalAutoLock AutoLock(*m_oYuvCCriticalSection);
            task_pack.buf = new char[m_oYuvBuf.size()];			//
            memcpy(task_pack.buf,m_oYuvBuf.data(),m_oYuvBuf.size());
            task_pack.len = m_oYuvBuf.size();
            if(m_oCameraCapture->Hight()*m_oCameraCapture->Width()*3/2 != m_oYuvBuf.size())
            {
                  int A =0;
            }
         }
         if(m_pHandler)
             m_pHandler->receive_handler((const char *)&task_pack,sizeof(task_pack));
     }
     return 0;
}

